package xyz.jcat.common.web;

import lombok.Getter;
import xyz.jcat.common.exception.BusinessException;

@Getter
public class RespException extends BusinessException {

    private IRespCodeMsg respCodeMsg;
    private Resp resp;
    private String errorMsg;

    public RespException(IRespCodeMsg respCodeMsg) {
        super(convertMessage(respCodeMsg.getCode(), respCodeMsg.getMsg()));
        this.respCodeMsg = respCodeMsg;
        this.resp = Resp.fail(respCodeMsg);
    }

    public RespException(IRespCodeMsg respCodeMsg, String errorMsg) {
        super(errorMsg);
        this.respCodeMsg = respCodeMsg;
        this.errorMsg = errorMsg;
        this.resp = Resp.fail(respCodeMsg, errorMsg);
    }

    public RespException(Resp resp) {
        super(convertMessage(resp.getCode(), resp.getMsg()));
        this.resp = resp;
    }

    private static String convertMessage(int code, String msg) {
        return code + ": " + msg;
    }
}
